package com.bytz.modules.cms.reseller.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bytz.common.api.vo.Result;
import com.bytz.modules.cms.common.constants.AdminAuthConstants;
import com.bytz.modules.cms.common.util.ExcelUtil;
import com.bytz.modules.cms.reseller.entity.CouponReseller;
import com.bytz.modules.cms.reseller.model.CouponResellerModel;
import com.bytz.modules.cms.reseller.service.IAdminCouponResellerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@RestController
@Api(tags = "优惠券")
@RequestMapping("/couponReseller")
@PreAuthorize("hasAuthority('" + AdminAuthConstants.CouponReseller.EDIT + "')")
public class CouponResellerController {
    @Autowired
    private IAdminCouponResellerService adminCouponResellerService;


    @ApiOperation(value = "分页查询", notes = "分页查询")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Result<IPage<CouponReseller>> page(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                              @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                                              CouponReseller couponReseller,
                                              HttpServletRequest req
                                              ) {
        IPage<CouponReseller> page = new Page<>(pageNo, pageSize);
         adminCouponResellerService.getCouponResellerPage(page, couponReseller,req);
        return Result.ok(page);
    }


    @ApiOperation(value = "添加优惠卷", notes = "添加优惠卷")
    @RequestMapping(value = "/addCouponReseller", method = RequestMethod.POST)
    public Result<Boolean> addCouponReseller(@Validated @RequestBody CouponResellerModel couponReseller) {
        adminCouponResellerService.addCouponReseller(couponReseller);
        return Result.ok("添加成功");
    }

    @ApiOperation(value = "根据ID删除优惠卷", notes = "根据ID删除优惠卷")
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result<Boolean> delete(@RequestParam String id) {
        return Result.ok(adminCouponResellerService.removeById(id));
    }

    @ApiOperation(value = "批量导入优惠卷", notes = "批量导入优惠卷")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request) {
        List<CouponReseller> models = ExcelUtil.importGroup(request, CouponReseller.class, null);
        adminCouponResellerService.importExcel(models);
        return Result.ok();
    }

    @ApiOperation(value = "获取导入模板", notes = "获取导入模板")
    @RequestMapping(value = "/template", method = RequestMethod.POST)
    public void template(HttpServletResponse response) throws Exception {
        ExcelUtil.exportTemp("导入优惠卷", "Sheet1", CouponReseller.class, response);
    }


    @ApiOperation(value = "通过id查询", notes = "通过id查询")
    @RequestMapping(value = "/queryById", method = RequestMethod.GET)
    public Result<CouponReseller> queryById(@RequestParam(value = "id") String id) {
        return Result.ok(adminCouponResellerService.getModelById(id));
    }
}
